/*
 * Created on 2005-1-25
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.nauu.atombuilder.commands;

import org.eclipse.draw2d.geometry.Point;
import org.eclipse.gef.commands.Command;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.widgets.Shell;

import com.nauu.atombuilder.editors.ClassWizard;
import com.nauu.atombuilder.modle.Diagram;
import com.nauu.atombuilder.modle.Dialog;
import com.nauu.atombuilder.modle.Node;
import com.nauu.atombuilder.util.Callback;

/**
 * @author nauu
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class CreateNodeCommand extends Command {
    protected Diagram diagram;

    protected Node node;

    protected Point location;

    private Shell shell;
    
    public CreateNodeCommand(Shell shell){
    	this.shell = shell;
    }
    
    // setters

    public void setDiagram(Diagram diagram) {
        this.diagram = diagram;
    }

    public void setNode(Node node) {
        this.node = node;
    }

    public void setLocation(Point location) {
        this.location = location;
    }

    public void execute() {
        if (this.location != null) {
            this.node.setLocation(this.location);
        }
        
        final Diagram dg = this.diagram;
        final Node nd = this.node;
        
        if(!(nd instanceof Dialog)){
        	
        	WizardDialog dialog = new WizardDialog(shell, new ClassWizard(new Callback() {
        		
        		@Override
        		public void execute(Object... obj) {
        			String path = (String)obj[0];
        			nd.setPath(path);
        			nd.setId(path);
        			dg.addNode(nd);
        			
        		}
        		
        	}));
        	dialog.open();
        }else{
			dg.addNode(nd);
        }
        
    }

    public String getLabel() {
        return "Create Node";
    }

    public void redo() {
        this.execute();
    }

    public void undo() {
        diagram.removeNode(node);
    }
}